//
//  OSCRouter.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/2.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCRouter.h"

@interface OSCRouter ()

@property (nonatomic, copy) NSDictionary *viewModelViewMappings; // viewModel到view的映射

@end

@implementation OSCRouter

+ (instancetype)sharedInstance {
    static OSCRouter *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (OSCBaseViewController *)viewControllerForViewModel:(OSCBaseViewModel *)viewModel {
    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    
    if (!viewController) {
        NSString *viewModelStr = NSStringFromClass(viewModel.class);
        NSString *prefixStr = [viewModelStr substringWithRange:NSMakeRange(0, viewModelStr.length - 5)];
        viewController = [prefixStr stringByAppendingString:@"Controller"];
    }
    
    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[OSCBaseViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);
    return [[NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}
- (NSDictionary *)viewModelViewMappings {
    return @{
             @"OSCBaseViewModel": @"OSCBaseViewController",
             @"OSCOAuthViewModel": @"OSCLoginViewController",
             @"OSCTweetListViewModel": @"OSCTweetTableViewController",
             @"OSCTweetDetailViewModel": @"OSCTweetDetailViewController",
             @"OSCMainViewModel": @"OSCMainViewController",
             @"OSCPageViewModel": @"OSCBasePageViewController",
             @"OSCBottomBarViewModel" : @"OSCBottomBarViewController"
             };
}
@end
